﻿namespace Hotel.Web.Controllers
{
    using System.Collections.Generic;
    using System.Web.Mvc;
    using Hotel.Module.Resources.Models;
    using Hotel.Web.Models;
    using ManagementModule = Hotel.Module.Management;
    using ManagementModels = Hotel.Module.Management.Models;
    using ResourcesModule = Hotel.Module.Resources;
    using Hotel.Web.Helpers;

    public class HallOrderController : Controller
    {
        //
        // GET: /HallOrder/
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(HallOrder order)
        {
            #region validation logic
            if (order.OutDate < order.InDate)
            {
                ModelState.AddModelError("", Hotel.Web.Views.OrderRoom.RoomOrder.OutDateError);
            }
            #endregion

            if (ModelState.IsValid)
            {
                CreateOrder(order);
                if (order.ID > 0)
                {
                    // Model is valid redirecting to Success Page
                    return View("SuccessOrder", order);
                }
                else // order.ID == -1 - error, order.ID == 0 - save failed
                {
                    ModelState.AddModelError("", Hotel.Web.Views.HallOrder.HallOrderResources.NotEnoughHallError);
                }
            }
            // Model is not valid!!! return page with errors
            return View(order);
        }

        public void CreateOrder(HallOrder order)
        {
            lock (Lockers._mutex)
            {
                using(ManagementModule.ManagementController m_ManagementController = Helpers.ServiceLocator.Instance.GetService(typeof(ManagementModule.IManagement)) as ManagementModule.ManagementController)
                using (ResourcesModule.HotelResourcesController m_ResourcesController = Helpers.ServiceLocator.Instance.GetService(typeof(ResourcesModule.IResourseManagment)) as ResourcesModule.HotelResourcesController)
                {
                    if (order is HallOrder)
                    {
                        ManagementModels.Order hallOrder = new ManagementModels.Order();
                        order.AssignTo(hallOrder);

                        HallType type = HallType.StandartHall;
                        switch (hallOrder.RoomType)
                        {
                            case ManagementModels.ItemTypes.Small: type = HallType.Small;
                                break;
                            case ManagementModels.ItemTypes.Big: type = HallType.Big;
                                break;
                            default: break;
                        }

                        List<Hall> freeRooms = m_ResourcesController.GetFreeHalls(hallOrder.InDate, hallOrder.OutDate, type);

                        if (freeRooms.Count >= hallOrder.NumberOfRooms)
                        {
                            order.ID = m_ManagementController.GetNewOrderNumber();
                            hallOrder.ID = order.ID;

                            Period period = new Period();
                            period.OrderID = order.ID;
                            period.InDate = hallOrder.InDate;
                            period.OutDate = hallOrder.OutDate;

                            for (int i = 0; i < hallOrder.NumberOfRooms; i++)
                            {
                                freeRooms[i].Periods.Add(period);
                                hallOrder.RoomPackage.Add(freeRooms[i].Number);
                            }
                            hallOrder.State = ManagementModels.OrderState.Preordered;
                            m_ManagementController.Storage.Add(hallOrder);
                        };

                    }
                    m_ManagementController.SaveChanges();
                    m_ResourcesController.SaveChanges();
                }
            }
            return;
        }
    }

}

